﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;

namespace ChromaCode.Extensions
{
    /// <summary>
    /// Extensions for TextRanges
    /// </summary>
    public static class TextRangeExtensions
    {
        /// <summary>
        /// Gets the next physical insertation point at the specified offset
        /// </summary>
        /// <param name="source">The source pointer to move forwards or backwards from</param>
        /// <param name="offset">The number of cgaracters to move forwards (or backwards if negative)</param>
        /// <returns></returns>
        public static TextPointer GetInsertationPointAtOffset(this TextPointer source, int offset)
        {
            TextPointer result =  source;

            if (offset > 0)
            {
                // Positive offset so move forwards
                for (int i = 0; i < offset; i++)
                {
                    result = result.GetNextInsertionPosition(LogicalDirection.Forward);
                }
            }
            else if (offset < 0)
            {
                // Negative offset so move backwards
                // Positive offset so move forwards
                for (int i = offset; i > 0; i--)
                {
                    result = result.GetNextInsertionPosition(LogicalDirection.Backward);
                }
            }

            return result;
        }
    }
}
